layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
 
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/shop/activity/activitygoods3.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
            var data_arr = [];
            //封装返回数据
			console.log(res);
            var j = 0;
			for (i in res.data) {
                if($.inArray(res.data[i].goods_id,data_gids) < 0)
                {
                 res.data[i]['reason'] = "<button class='layui-btn purchase' gid='"+j+"'>确定</button>";
                  data_arr.push(res.data[i]);
                  j++;
                }
            }
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": data_arr //解析数据列表
			};
		}
		,cols: [[
		  {field:'goods_id', title: '序号'}
		  ,{field:'goods_name', title: '产品名称'}
		  ,{field:'brand_name', title: '品牌'}
		  ,{field:'marker', title: '型号'}
		  ,{field:'memory_name', title: '内存'}
		  ,{field:'attribute_name', title: '属性'}
		  ,{field:'colour_name', title: '颜色'}
		  ,{field:'selling_price', title: '零售单价（元）'}
		  ,{field:'trade_price', title: '批发价（元）'}
          ,{field:'buying_price', title: '进货单价（元）'}
          ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
        ,done: function(res, curr, count){
             $('.purchase').on('click',function(){
                   var id = $(this).attr('gid');
                   var info = res.data[id];
                   selection([info]);
                   $(this).closest('tr').remove();
             })
        }
	  });
	}
	tableInit();
    
    var data_selection = [];
    var data_gids = [];
    function selection(info = [])
    {
        if(info != [])
        {
            for (i in info)
            {
                info[i]['reason'] = "<button class='layui-btn selection-cancel' gid='"+info[i]['goods_id']+"'>取 消</button>";
                data_selection.push(info[i]);
                data_gids.push(info[i].goods_id);
            }
        }
        //展示已知数据
          table.render({
            elem: '#dataTables-selection'
            ,cols: [[
              {field:'goods_id', title: '序号'}
              ,{field:'goods_name', title: '产品名称'}
              ,{field:'brand_name', title: '品牌'}
              ,{field:'marker', title: '型号'}
              ,{field:'memory_name', title: '内存'}
              ,{field:'attribute_name', title: '属性'}
              ,{field:'colour_name', title: '颜色'}
              ,{field:'selling_price', title: '零售单价（元）'}
              ,{field:'trade_price', title: '批发价（元）'}
              ,{field:'buying_price', title: '进货单价（元）'}
              ,{field:'reason', title: '操作'}
            ]]
            ,data: data_selection
            //,skin: 'line' //表格风格
            //,even: true
            //,page: true //是否显示分页
            //,limits: [5, 7, 10]
            //,limit: 5 //每页默认显示的数量
          });
    }
    selection(parent.open_data());
    $('body').on('click', '.selection-cancel', function(){
        var gid = $(this).attr('gid');
        var data_sel = [];
        var data_gid = [];
        var gids = '';
        for (i in data_selection) {
                if(data_selection[i]['goods_id'] != gid)
                {
                    data_sel.push(data_selection[i]);
                    data_gid.push(data_selection[i]['goods_id']);
                    gids += data_selection[i]['goods_id'] + ',';
                }
			}
         gids = gids.substr(0, gids.length - 1);
         data_selection = data_sel;
         data_gids = data_gid;
		$(this).closest('tr').remove();
        tableInit({gids:gids});
	});
	//监听提交
	form.on('submit(sreach)',
	function(data) {
		tableInit(data.field);
		return false;
	});
    /*关闭页面*/
    form.on('submit(close)',
    function(data) {
        //关闭当前frame
        xadmin.close();
        return false;
    });
    /*确定选中商品*/
    form.on('submit(sure)',
    function(data) {
        parent.GetValue(data_selection);
        //关闭当前frame
        xadmin.close();
        return false;
    });
});

